package com.skytech.component.crawler.web;

import com.skytech.component.crawler.scratch.base.CreatePageType;
import com.skytech.component.crawler.scratch.base.TargetRules;
import com.skytech.component.crawler.scratch.base.TargetRulesDAO;
import com.skytech.component.crawler.word.HtmlToWordService;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.OutputStream;
import java.util.Comparator;
import java.util.List;

@RestController
public class CrawlerConfigController {
    @Autowired
    private TargetRulesDAO targetRulesDAO;

    @Autowired
    private HtmlToWordService htmlToWordService;
    @GetMapping("crawler/config/getByType/{type}")
    public List<TargetRules> getConfig(@PathVariable String type){
        TargetRules targetRules = new TargetRules();
        if(!"all".equals(type))
        targetRules.setType(CreatePageType.valueOf(type).toString());
        List<TargetRules> all = targetRulesDAO.findAll(Example.of(targetRules));
        all.sort(Comparator.comparing(TargetRules::getId));
        return all;
    }

    @PutMapping("crawler/config/saveOrUpdate")
    public TargetRules updateConfig(@RequestBody TargetRules targetRules){
        return targetRulesDAO.save(targetRules);
    }
    @GetMapping("doc/download")
    public void download(HttpServletRequest request, HttpServletResponse response){
        htmlToWordService.createWord(request,response);


    }

}
